package com.example.multiplesprite;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Bitmap;

public class GameController extends SurfaceView {

    private SurfaceHolder _surfaceHolder;
    private GameThread _gameThread;
    private List<Character> Character = new ArrayList<Character>();
    public GameController(Context context) {

          super(context);
          _gameThread = new GameThread(this);
          _surfaceHolder = getHolder();
          _surfaceHolder.addCallback(new SurfaceHolder.Callback() {
          @Override
           public void surfaceDestroyed(SurfaceHolder _surfaceHolder) {
                  boolean retry = true;
                  _gameThread.setRunning(false);
                   while (retry) {
                      try {
                    	  _gameThread.join();
                   retry = false;
                   } catch (InterruptedException e) {
                 }
                 }
           }
          @Override
           public void surfaceCreated(SurfaceHolder _surfaceHolder) {
                        createSprites();
                        _gameThread.setRunning(true);
                        _gameThread.start();
            }
            @Override
            public void surfaceChanged(SurfaceHolder _surfaceHolder, int format,
                      int width, int height) {
             }
          });
    }

    private void createSprites() {
    	Character.add(createSprite(R.drawable.boy1));
    	//Character.add(createSprite(R.drawable.girl1));
    	//Character.add(createSprite(R.drawable.girl2));

    }   

    private Character createSprite(int resouce) {
          Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
          return new Character(this,bmp);
    }

    @Override
    protected void onDraw(Canvas canvas) {
          canvas.drawColor(Color.BLACK);
          for (Character sprite : Character) {
                 sprite.onDraw(canvas);
          }
    }

}